//
//  SharView.m
//  ShrimpCrowdordering
//
//  Created by 骆锴 on 2018/8/27.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ShareURLView.h"

@implementation ShareURLView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.shadowBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.shadowBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        self.shadowBtn.alpha = 1;
        [self.shadowBtn addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.shadowBtn];
        
        UIView *baseview = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(38), ScreenX375(196), ScreenX375(300), ScreenX375(200))];
        baseview.layer.cornerRadius = 6.0;
        baseview.backgroundColor = [UIColor whiteColor];
        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(26), ScreenX375(76), ScreenX375(50), ScreenX375(14))];
        label1.text = @"链接:";
        label1.textColor = REXADECIMALCOLOR(@"#212121");
        label1.font = Kfont(15);
        [baseview addSubview:label1];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(26), ScreenX375(110), ScreenX375(50), ScreenX375(14))];
        label2.text = @"密码:";
        label2.textColor = REXADECIMALCOLOR(@"#212121");
        label2.font = Kfont(15);
        [baseview addSubview:label2];
        
        
        _url = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(80), ScreenX375(76), ScreenX375(250), ScreenX375(15))];
        _url.text = @"http://www.baiduiyunpan";
        _url.textColor = REXADECIMALCOLOR(@"#6BA4E8");
        _url.font = Kfont(15);
        _url.userInteractionEnabled = YES;
        [baseview addSubview:_url];
        
        _passWord = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(80), ScreenX375(110), ScreenX375(250), ScreenX375(15))];
        _passWord.text = @"8jekek";
        _passWord.textColor = REXADECIMALCOLOR(@"#6BA4E8");
        _passWord.font = Kfont(15);
        _passWord.userInteractionEnabled = YES;
        [baseview addSubview:_passWord];
        
        UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(277), ScreenX375(10), ScreenX375(13), ScreenX375(13))];
        [close setBackgroundImage:[UIImage imageNamed:@"评论叉"] forState:UIControlStateNormal];
        [close addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        [baseview addSubview:close];
        
        [self addSubview:baseview];
        
        return self;
    }
}

-(void)setUrl:(NSString *)url andPassword:(NSString *)password{
    _url.text = url;
    _passWord.text = password;
}

-(void)hidden{
    if (_hiddenClick) {
        _hiddenClick();
    }
}

-(void)attachTapHandler {
    UILongPressGestureRecognizer *touch1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
     UILongPressGestureRecognizer *touch2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    
    [_url addGestureRecognizer:touch1];
    [_passWord addGestureRecognizer:touch2];
}

-(void)handleTap:(UIGestureRecognizer*) recognizer {
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        return;
    }else if (recognizer.state == UIGestureRecognizerStateBegan){
        [self becomeFirstResponder];
        self.backgroundColor = REXADECIMALCOLOR(@"#ECECEC");
        UILabel *label = (UILabel *)recognizer.view;
        if (label == _passWord) {
            UIMenuItem * item = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(newFunc1)];
            [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
            [UIMenuController sharedMenuController].menuItems = @[item];
            [UIMenuController sharedMenuController].menuVisible = YES;
        }else{
            UIMenuItem * item = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(newFunc2)];
            [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
            [UIMenuController sharedMenuController].menuItems = @[item];
            [UIMenuController sharedMenuController].menuVisible = YES;
        }
       
    }
}

-(void)newFunc1{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = _passWord.text;
}
-(void)newFunc2{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = _url.text;
}

@end
